﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HPIT.Web
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            //1. 收集用户输入的信息
            string userName = txt_UserName.Value.Trim(); //用户名
            string email = txt_Email.Value.Trim();//邮箱
            string password = txt_Password.Value.Trim();//密码
            string passwordConfirm = txt_Password_Confirm.Value.Trim();//确认密码

            //2. 准备连接字符串和SQL语句
            //2.1 声明数据库连接字符串


            #region 1 声明数据库连接字符串


            #endregion


            string sql = string.Empty;

            #region 检测用户名是否存在

            //2.2定义查询用户名是否存在的SQL语句
            sql = @"SELECT COUNT(*) FROM dbo.UserInfo WHERE UserName=@UserName";

            //2.3 定义参数
            SqlParameter[] pams = {
                new SqlParameter("@UserName",userName)
            };


            if (Convert.ToInt32(DbHelper.ExecuteScalar(sql, pams)) > 0)
            {
                lbl_Msg.Text = "该用户已存在，请换一个用户名";
                return;
            }
           
            #endregion
            
            //2.2 声明SQL语句
            sql = @"INSERT dbo.UserInfo ( UserName, [Password], Email, AddTime ) VALUES  ( @UserName,  @Password, @Email, @AddTime )";

            //2.3 定义参数
            SqlParameter[] pams2 = {
                new SqlParameter("@UserName",userName),
                new SqlParameter("@Password",password),
                new SqlParameter("@Email",email),
                new SqlParameter("@AddTime",DateTime.Now)
            };
            if (DbHelper.ExecuteNonQuery(sql, pams2) > 0)
            {
                lbl_Msg.Text = "注册成功";
                txt_UserName.Value = string.Empty;
                txt_Email.Value = string.Empty;
            }
            else
            {
                lbl_Msg.Text = "注册失败，请稍后重试";
            }
        }

       
    }
}